package com.css.dzj.dzjportal.modules.interflow.controller;

import com.css.dzj.dzjportal.common.util.BeanUtil;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.modules.interflow.entity.AlInterflowEntity;
import com.css.dzj.dzjportal.modules.interflow.service.InterflowService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 公众交流Controller
 * @author wt
 * @date 2020\3\10 0010
 */
@RestController
@RequestMapping("/interflow")
public class InterflowController {

    @Autowired
    private InterflowService ifService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){

        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<AlInterflowEntity> interResults = ifService.queryPage(pageRequest);
            return R.ok().put("data", interResults);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }

    }


    /**
     * 信息
     */
    @GetMapping("/info")
    public R info(@RequestParam String id){

        AlInterflowEntity interflowEntity = ifService.findById(id);
        return R.ok().put("data", interflowEntity);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AlInterflowEntity interflowEntity){

        try {
            if(StringUtils.isEmpty(interflowEntity)) {
                return R.error("公众交流信息不能为空");
            }
            ifService.add(interflowEntity);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改和反馈
     */
    @PostMapping("/update")
    public R update(@RequestBody AlInterflowEntity interflowEntity){

        try {
            AlInterflowEntity oldinterflowEntity = ifService.findById(interflowEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(interflowEntity, oldinterflowEntity);
            oldinterflowEntity.setStatte("1");
            oldinterflowEntity.setFeedbackTime(new Date());
            oldinterflowEntity.setUpdateTime(new Date());
            ifService.update(oldinterflowEntity);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestParam String id){

        try {
            if(ifService.delete(id)){
                return R.ok("操作成功!");
            }else{
                return  R.error("操作失败!");
            }
        } catch(Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     *通过咨询建议查询公众交流信息
     */
    @GetMapping("/gzjlList")
    public R gzjlList(@RequestParam String flowSuggest,
                      @RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<AlInterflowEntity> interflowResults = ifService.gzjlList(flowSuggest,pageRequest);
            return R.ok().put("data", interflowResults);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


}
